package com.aube.plugin.origin;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aube.plugin.wap.H5WrapperActivity;
import com.huyn.baseframework.dynamicload.DLBridge;
import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.IPositionManager;
import com.huyn.baseframework.utils.StringUtils;
import com.ryg.dynamicload.internal.DLIntent;

/**
 * Created by huyaonan on 16/5/12.
 */
public class ScrollHeader extends DLLayout {
    private TextView mTitle, mDesc;
    private ImageView mPlayIcon;
    public ScrollHeader(Context context) {
        this(context, null);
    }
    public ScrollHeader(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public ScrollHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void fillData(Object o) {

    }

    @Override
    public void initViews() {
        inflate(R.layout.item_header, this, true);
        mTitle = (TextView) findViewById(R.id.header_title);
        mDesc = (TextView) findViewById(R.id.header_desc);
        mPlayIcon = (ImageView) findViewById(R.id.header_play);

        setImgWithRes(mPlayIcon, R.drawable.icon_new_play);

        mPlayIcon.setAlpha(0f);
        mTitle.setAlpha(0f);
        mDesc.setAlpha(0f);
        mPlayIcon.setVisibility(View.GONE);
        mTitle.setVisibility(View.GONE);
        mDesc.setVisibility(View.GONE);
    }

    private void setText(TextView textView, String text) {
        if(StringUtils.isNotBlank(text)) {
            textView.setText(text);
            textView.setVisibility(View.VISIBLE);
        }
    }

    public void fillData(final DataConvertUtil.SimpleIndexHeader header, final DLBridge listener) {
        if(header.type.equalsIgnoreCase("video")) {
            this.mTitle.setText(header.title);
            this.mDesc.setText(header.desc);
            setText(this.mTitle, header.title);
            setText(this.mDesc, header.desc);
            mPlayIcon.setVisibility(View.VISIBLE);

            setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener != null)
                        listener.onClickToVideo(header.channel);
                }
            });
        } else {
            setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener != null) {
//                        listener.onClickToWap(header.title, header.url, true);
                        DLIntent intent = new DLIntent("com.aube.plugin.origin", "com.aube.plugin.wap.H5WrapperActivity");
                        intent.putExtra(H5WrapperActivity.WEB_POST, true);
                        intent.putExtra(H5WrapperActivity.WEB_TITLE, header.title);
                        intent.putExtra(H5WrapperActivity.WEB_URL, header.url);
                        listener.openPluginActivity(intent);
                    }
                }
            });
        }
    }

    public void startAnimIn() {
        final int width = mTitle.getWidth();
        final int widthDesc = mDesc.getWidth();
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1f);
        valueAnimator.setDuration(500);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mPlayIcon.setAlpha(value);
                int degree = (int) (360*value);
                mPlayIcon.setRotation(degree);
                mPlayIcon.setScaleX(value);
                mPlayIcon.setScaleY(value);
                mDesc.setAlpha(value);
                mDesc.setTranslationX(widthDesc*(1-value)/2);
                mTitle.setAlpha(value);
                mTitle.setTranslationX(width*(1-value)/2);
            }
        });
        valueAnimator.start();
    }

    @Override
    public void notifyAnim(int i) {

    }

    @Override
    public void registToManager(IPositionManager iPositionManager) {

    }

    @Override
    public void unreistFromManager(IPositionManager iPositionManager) {

    }

    @Override
    public int[] getLocations() {
        return new int[0];
    }
}
